package cn.codeforfun.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

/**
 * Package cn.codeforfun.config
 * Project SpringBootMail
 * Author WangBin
 * Email bin.wang12@pactera.com
 * Created on 2017/6/9 16:49
 * version 1.0.0
 */
@Configuration
public class ThymeleafConfig {
  @Bean
  public ITemplateResolver getTemplateResolver() {
    ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
    resolver.setPrefix("templates/");
    resolver.setCacheable(false);
    resolver.setCharacterEncoding("UTF-8");
    resolver.setTemplateMode("HTML5");
    return resolver;
  }

  @Bean
  public TemplateEngine getTemplateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setTemplateResolver(getTemplateResolver());
    return engine;
  }

  @Bean
  public ViewResolver getViewResolver() {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(getTemplateEngine());
    resolver.setCharacterEncoding("UTF-8");
    return resolver;
  }
}
